package code;

import java.text.DecimalFormat;

public class WeightController {
	private double tara;
	private double netto = 0;
	private double brutto = 0;
	private String[] display = {"0.000 kg","",""};
	private char type;
	private String text1;
	private String text2;
	private String text3;

	//aktøren indtaster kommandoen, og menuen returnerer et mellemrum, så aktøren kan indtaste kommandoen efterfølgende.
	public String parseCommand(String input){

		String startPart = null;
		String endPart = null;

		if(input.indexOf(" ") > -1){
			startPart = input.substring(0, input.indexOf(" "));
			endPart = input.substring(input.indexOf(" ") + 1);

		}
		else{
			startPart = input;
		}
		// checker om input er en RM20 besked, og tildeler parameterne en variable


		switch (startPart)
		{
		case "S":
			return command_S();
		case "T": 
			return command_T();
		case "D": 
			return command_D(endPart);
		case "DW": 
			return command_DW(endPart);
		case "P111": 
			return command_P111(endPart);
		case "RM20":
			return command_RM20(endPart);
		case "B":
			return command_B(endPart);
		case "Q":
			return command_Q();
		default:
			return "E S";
		}
	}


	private String command_S() 
	{
		return "S S      " + getNetto(brutto, tara) + " kg";
	}

	private String command_T() 
	{ 

		tara = tara + netto;

		getNetto(brutto, tara);

		return "T S      " + tara + " kg";
	}

	private String command_D(String endPart) 
	{

		if((endPart== null)||(endPart.charAt(0) != '\"') || (endPart.charAt(endPart.length()-1) != '\"')){

			return "E S";
		}
		endPart = endPart.substring(1,endPart.length()-1);

		if(endPart.length() > 7){
			endPart = endPart.substring(endPart.length() -7, endPart.length());
			display[0] = "* " + endPart;
		}
		else{
			display[0] = endPart;
		}

		return "D A";
	}

	private String command_DW(String endPart) 
	{
		if(endPart != null){

			return "E S";
		}
		else {

			getNetto(brutto, netto);

			return "DW A";
		}

	}

	private String command_P111(String endPart)
	{

		if(endPart.length() > 32){ // længden må maks være 32, fordi at 2 ", skal klippes væk
			return "E S";
		}

		if((endPart.charAt(0) != '\"') || (endPart.charAt(endPart.length()-1) != '\"'))
		{
			return "E S";
		}

		endPart = endPart.substring(1,endPart.lastIndexOf("\""));
		display[1] = endPart;

		return "A";
	}

	private String command_RM20(String endPart)
	{
		if(endPart == null){
			return "RM20 L";
		}
		
		String[] tekst = endPart.split("\"");
		if(tekst.length == 6){
			type = endPart.charAt(0);
			text1 = tekst[1];
			text2 = tekst[3];
			text3 = tekst[5];
		}
			
			if((tekst.length != 6) || (text1.length() > 24 ) || (text2.length() > 24) || (endPart.charAt(endPart.length()-1)!= '\"')||(type != '8')||(!text3.equals("&3")))
			{
				return "RM20 L";
			}
			

		


		display[2] = text1 + " " + text2;

		return "RM20 B";
	}

	private String command_B(String endPart)
	{
		if(endPart == null){
			return "E S";
		}
		try {
			brutto = Double.parseDouble(endPart);
		} catch (NumberFormatException e) {}
		
		if(brutto > 6.000){
			command_D("\"error\"");
			return "Error";

		}
		else{

			getNetto(brutto, tara);

			return "DB ";
		}
	}

	private String command_Q()
	{
		return "Q "; 
	}

	public double getNetto(double brutto, double tara)
	{
		String nettoString;
		netto = brutto - tara;


		nettoString = new DecimalFormat( "0.000").format(netto);
		nettoString = nettoString.replaceAll(",", ".");
		display[0] = nettoString + " kg";
		netto = Double.parseDouble(nettoString);

		return netto;
	}

	public String getDisplay(int displayNumber)
	{
		return display[displayNumber-1];
	}
	public void updateDisplay(){

		display[2] = "";
	}

}
